/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.manage;

import controller.MySQL;
import controller.insert.InsertController;
import controller.insert.InsertController.InsertListener;
import controller.query.QueryController;
import controller.query.QueryController.QueryListener;
import controller.update.UpdateController;
import controller.update.UpdateController.UpdateListener;
import hibernate.dao.EnderecoFuncionario;
import hibernate.dao.Funcionario;
import hibernate.dao.TelefoneFuncionario;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import view.ViewUtil;

/**
 *
 * @author Felipe
 */
public class ViewManageFuncionario extends ViewBaseUpdate {

    /**
     * Creates new form ViewUpdateFuncionario
     */
    private static ViewManageFuncionario sViewUpdateFuncionario = null;
    
    private ArrayList<Funcionario> mFuncionarios = null;
    private ArrayList<TelefoneFuncionario> mTelefones = null;
    private Funcionario mSelectedFuncionario = null;   
    private TelefoneFuncionario mSelectedTelefone = null;    
    
    private UpdateController mTelefoneUpdateController  = new UpdateController(new UpdateListener() {

        @Override
        public void onUpdateComplete() {
            JOptionPane.showMessageDialog(ViewManageFuncionario.this, "Telefones atualizados com sucesso.");
            selecionaTelefones();
        }

        @Override
        public void onUpdateError(String message, Exception e) {
            if (e == null) {
                JOptionPane.showMessageDialog(ViewManageFuncionario.this, message);
            } else {
                MySQL.handleException(e);
            }
        }
    });
    
    private InsertController mTelefonesInsertController = new InsertController(new InsertListener() {
        @Override
        public void onInsertComplete() {
            JOptionPane.showMessageDialog(ViewManageFuncionario.this, "Telefone adicionado com sucesso.");
            selecionaTelefones();
        }
        @Override
        public void onInsertError(Exception e) {
            MySQL.handleException(e);
        }
    });
    private QueryController mTelefonesQueryController = new QueryController(new QueryListener() {
        @Override
        public void onQueryComplete(List pList) {
            List<TelefoneFuncionario> telefones = (List<TelefoneFuncionario>) pList;
            mTelefones = new ArrayList();
            comboBoxTelefones.removeAllItems();
            for (TelefoneFuncionario t : telefones) {
                mTelefones.add(t);
                comboBoxTelefones.addItem(t.getTelCliNumero());                
            }
        }
        @Override
        public void onQueryError(Exception e) {
            MySQL.handleException(e);
        }
    });    
    
    private ViewManageFuncionario() {
        super("From Funcionario where func_flag = 1");
        initComponents();
        ViewUtil.centralizaComponente(this);
        select();
        ListSelectionModel list = mTable.getSelectionModel();
        list.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                fillFields();
            }
        });
        comboBoxTelefones.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                int index = comboBoxTelefones.getSelectedIndex();
                if (index < 0 || mTelefones == null || comboBoxTelefones.getItemCount() <= 0) return;
                mSelectedTelefone = mTelefones.get(index);
                comboBoxTipo.setSelectedItem(mSelectedTelefone.getTelFunTipo());
                txtTelefone.setText(mSelectedTelefone.getTelCliNumero());
            }
        });
        comboBoxFiltro.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                filterSelect();
            }
        });
    }
    
    public static ViewManageFuncionario getInstance() {
        return (sViewUpdateFuncionario == null) ? sViewUpdateFuncionario = new ViewManageFuncionario() : sViewUpdateFuncionario;
    }
        
    @Override
    protected void onTableQueryComplete(List pList) {        
        List<Funcionario> funcionarios = (List<Funcionario>)pList;
        DefaultTableModel model = (DefaultTableModel) mTable.getModel();  
        model.setNumRows(0);   
        mFuncionarios = new ArrayList();     
        for(Funcionario f : funcionarios) { 
            f.getEnderecoFuncionario().getEndFuncEstado().toString();
            f.getEnderecoFuncionario().getEndFuncCep().toString();
            f.getEnderecoFuncionario().getEndFuncCidade().toString();
            f.getEnderecoFuncionario().getEndFuncNumero().toString();
            f.getEnderecoFuncionario().getEndFuncRua().toString();
            mFuncionarios.add(f);
            model.addRow(new Object[]{f.getFuncNome(), f.getFuncCpf(), f.getFuncRg(), f.getFuncTipo(), f.getFuncValorHora()});
        }        
    }
    
    public void fillFields() {
        if (mTable.getSelectedRow() < 0) {
            clearFields(false);
            return;
        }
        mSelectedFuncionario = mFuncionarios.get(mTable.getSelectedRow());
        EnderecoFuncionario end = mSelectedFuncionario.getEnderecoFuncionario();
        
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(mSelectedFuncionario.getFuncDataInicio());
        
        txtNome.setText(mSelectedFuncionario.getFuncNome());
        txtRG.setText(mSelectedFuncionario.getFuncRg());
        txtCPF.setText(mSelectedFuncionario.getFuncCpf());
        txtTipo.setText(mSelectedFuncionario.getFuncTipo());
        txtValorHora.setText(String.valueOf(mSelectedFuncionario.getFuncValorHora()));
        txtDataInicio.setText(calendar.get(Calendar.DATE) + "/" + calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.YEAR));
        txtRua.setText(mSelectedFuncionario.getEnderecoFuncionario().getEndFuncRua());
        txtNumero.setText(mSelectedFuncionario.getEnderecoFuncionario().getEndFuncNumero().toString());
        txtCEP.setText(mSelectedFuncionario.getEnderecoFuncionario().getEndFuncCep().toString());
        txtCidade.setText(mSelectedFuncionario.getEnderecoFuncionario().getEndFuncCidade());
        comboBoxEstado.setSelectedItem(end.getEndFuncEstado().toString());
        
        selecionaTelefones();
    }

    @Override
    protected void clearFields(boolean clearFilter) {  
        if (clearFilter)
            txtTipo.setText("");
        txtValorHora.setText("");
        txtDataInicio.setText("");
        txtNome.setText("");
        txtRG.setText("");
        txtCPF.setText("");
        txtRua.setText("");
        txtNumero.setText("");
        txtCEP.setText("");
        txtCidade.setText("");
        comboBoxEstado.setSelectedIndex(0);
        txtTelefone.setText("");
        comboBoxTelefones.removeAllItems();
        comboBoxTipo.setSelectedIndex(0);
        comboBoxFiltro.setSelectedIndex(0);
    }
    
    private void selecionaTelefones() {
        mTelefonesQueryController.select("From TelefoneFuncionario where Funcionario_FUNC_ID = " + mSelectedFuncionario.getFuncId());        
    }
    
    private void filterSelect() {
        final int index = comboBoxFiltro.getSelectedIndex();
        final String text = txtFiltro.getText();
        if (index == 0) {
            select("From Funcionario where func_flag = 1 and func_nome like '%" + text + "%'");
            return;
        }
        if (index == 1) {
            select("From Funcionario where func_flag = 1 and func_cpf like '%" + text + "%'");
            return;
        }
        if (index == 2) {
            select("From Funcionario where func_flag = 1 and func_rg like '%" + text + "%'");
            return;
        }
        if (index == 3) {
            select("From Funcionario where func_flag = 1 and func_tipo like '%" + text + "%'");
        }
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        mTable = new javax.swing.JTable();
        txtFiltro = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        comboBoxFiltro = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        txtNome = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtRG = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtRua = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        comboBoxTelefones = new javax.swing.JComboBox();
        comboBoxTipo = new javax.swing.JComboBox();
        btnExcluirTelefone = new javax.swing.JButton();
        btnSalvarTelefone = new javax.swing.JButton();
        btnAddTelefone = new javax.swing.JButton();
        txtTelefone = new javax.swing.JFormattedTextField();
        jLabel11 = new javax.swing.JLabel();
        txtCidade = new javax.swing.JTextField();
        txtCPF = new javax.swing.JFormattedTextField();
        txtCEP = new javax.swing.JFormattedTextField();
        txtNumero = new javax.swing.JFormattedTextField();
        comboBoxEstado = new javax.swing.JComboBox();
        jLabel12 = new javax.swing.JLabel();
        txtTipo = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        txtDataInicio = new javax.swing.JFormattedTextField();
        txtValorHora = new javax.swing.JFormattedTextField();
        btnExcluir = new javax.swing.JButton();
        btnSalvar = new javax.swing.JButton();

        setTitle("Gerenciar Funcionários");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Informações básicas"));

        mTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Nome", "CPF", "RG", "Tipo", "Valor/Hora"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        mTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(mTable);

        txtFiltro.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtFiltroKeyReleased(evt);
            }
        });

        jLabel1.setText("Filtrar");

        comboBoxFiltro.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nome", "CPF", "RG", "Tipo" }));

        jLabel2.setText("Campo");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtFiltro, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(comboBoxFiltro, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 535, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFiltro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(comboBoxFiltro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Informações completas"));

        jLabel3.setText("Nome");

        jLabel4.setText("CPF");

        jLabel6.setText("RG");

        jLabel7.setText("Rua");

        jLabel8.setText("Número");

        jLabel9.setText("CEP");

        jLabel10.setText("Estado");

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Telefones"));

        comboBoxTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Fixo", "Móvel" }));

        btnExcluirTelefone.setText("Excluir Telefone");
        btnExcluirTelefone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirTelefoneActionPerformed(evt);
            }
        });

        btnSalvarTelefone.setText("Alterar Telefone");
        btnSalvarTelefone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarTelefoneActionPerformed(evt);
            }
        });

        btnAddTelefone.setText("Adicionar Telefone");
        btnAddTelefone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddTelefoneActionPerformed(evt);
            }
        });

        try {
            txtTelefone.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##########")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(comboBoxTelefones, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnSalvarTelefone, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnAddTelefone, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(comboBoxTipo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnExcluirTelefone, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                    .addComponent(txtTelefone))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboBoxTelefones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboBoxTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAddTelefone)
                    .addComponent(txtTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnExcluirTelefone)
                    .addComponent(btnSalvarTelefone))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        jLabel11.setText("Cidade");

        try {
            txtCPF.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            txtCEP.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("#####-###")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        txtNumero.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));

        comboBoxEstado.setModel(new javax.swing.DefaultComboBoxModel(controller.ItemsEstado.ESTADOS));

        jLabel12.setText("Tipo");

        jLabel13.setText("Valor/Hora");

        jLabel14.setText("Data Início");

        txtDataInicio.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter()));

        txtValorHora.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel3)
                            .addComponent(jLabel10)
                            .addComponent(jLabel11)
                            .addComponent(txtCidade, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                            .addComponent(comboBoxEstado, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel12)
                            .addComponent(jLabel7))
                        .addGap(7, 7, 7)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txtNome, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
                            .addComponent(txtTipo, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtRua, javax.swing.GroupLayout.Alignment.LEADING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel13)
                                    .addComponent(jLabel8)
                                    .addComponent(jLabel4)
                                    .addComponent(txtNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel5))
                            .addComponent(txtCPF)
                            .addComponent(txtValorHora))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtCEP, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtRG)
                            .addComponent(jLabel9)
                            .addComponent(jLabel6)
                            .addComponent(jLabel14)
                            .addComponent(txtDataInicio))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtCPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtRG, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel12)
                            .addComponent(jLabel14))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtDataInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtValorHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtRua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtCEP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(44, 44, 44)
                        .addComponent(jLabel13)
                        .addGap(44, 44, 44)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel11)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(19, 19, 19)
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(comboBoxEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnExcluir.setText("Excluir");
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });

        btnSalvar.setText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnSalvar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnExcluir)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnExcluir)
                    .addComponent(btnSalvar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddTelefoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddTelefoneActionPerformed
        TelefoneFuncionario telefone = new TelefoneFuncionario(mSelectedFuncionario, comboBoxTipo.getSelectedItem().toString(), txtTelefone.getText().trim());
        mTelefonesInsertController.insert(telefone);
    }//GEN-LAST:event_btnAddTelefoneActionPerformed

    private void btnSalvarTelefoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarTelefoneActionPerformed
        mSelectedTelefone.setTelCliNumero(txtTelefone.getText().trim());
        mSelectedTelefone.setTelFunTipo(comboBoxTipo.getSelectedItem().toString());
        mTelefoneUpdateController.update(mSelectedTelefone);
    }//GEN-LAST:event_btnSalvarTelefoneActionPerformed

    private void btnExcluirTelefoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirTelefoneActionPerformed
        mTelefoneUpdateController.delete(mSelectedTelefone);       
    }//GEN-LAST:event_btnExcluirTelefoneActionPerformed

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        if (mSelectedFuncionario == null || mTable.getSelectedRow() < 0) return;
                
        mSelectedFuncionario.setFuncNome(txtNome.getText().trim());
        mSelectedFuncionario.setFuncCpf(txtCPF.getText().trim());        
        mSelectedFuncionario.setFuncRg(txtRG.getText().trim());
        mSelectedFuncionario.setFuncTipo(txtTipo.getText().trim());
        mSelectedFuncionario.setFuncValorHora(Float.valueOf(txtValorHora.getText().trim().replaceAll(",", ".")));        
        try {
            mSelectedFuncionario.setFuncDataInicio(new Date(new SimpleDateFormat("dd/MM/yyyy").parse(txtDataInicio.getText()).getTime()));
        } catch (ParseException ex) {
        }
        mSelectedFuncionario.getEnderecoFuncionario().setEndFuncRua(txtRua.getText());
        mSelectedFuncionario.getEnderecoFuncionario().setEndFuncNumero(Integer.valueOf(txtNumero.getText().trim()));
        mSelectedFuncionario.getEnderecoFuncionario().setEndFuncCep(txtCEP.getText().trim());
        mSelectedFuncionario.getEnderecoFuncionario().setEndFuncCidade(txtCidade.getText());
        mSelectedFuncionario.getEnderecoFuncionario().setEndFuncEstado(comboBoxEstado.getSelectedItem().toString());
        
        mUpdateController.update(mSelectedFuncionario, mSelectedFuncionario.getEnderecoFuncionario());
    }//GEN-LAST:event_btnSalvarActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
        if (mSelectedFuncionario == null || mTable.getSelectedRow() < 0) return;
        
        mSelectedFuncionario.setFuncFlag(false);
        
        mUpdateController.update(mSelectedFuncionario);
    }//GEN-LAST:event_btnExcluirActionPerformed

    private void txtFiltroKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtFiltroKeyReleased
        filterSelect();
    }//GEN-LAST:event_txtFiltroKeyReleased

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddTelefone;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnExcluirTelefone;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JButton btnSalvarTelefone;
    private javax.swing.JComboBox comboBoxEstado;
    private javax.swing.JComboBox comboBoxFiltro;
    private javax.swing.JComboBox comboBoxTelefones;
    private javax.swing.JComboBox comboBoxTipo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable mTable;
    private javax.swing.JFormattedTextField txtCEP;
    private javax.swing.JFormattedTextField txtCPF;
    private javax.swing.JTextField txtCidade;
    private javax.swing.JFormattedTextField txtDataInicio;
    private javax.swing.JTextField txtFiltro;
    private javax.swing.JTextField txtNome;
    private javax.swing.JFormattedTextField txtNumero;
    private javax.swing.JTextField txtRG;
    private javax.swing.JTextField txtRua;
    private javax.swing.JFormattedTextField txtTelefone;
    private javax.swing.JTextField txtTipo;
    private javax.swing.JFormattedTextField txtValorHora;
    // End of variables declaration//GEN-END:variables
    
}